<template>
  <div>
    <search-pane
      :show="showSearchPane"
      @search="handleSearchLabManager"
      @reset="resetLabManagerQueryParams"
    >
      <search-item>
        <PDInput
          v-model="queryParams.labManagerId"
          input-width="200px"
          description="工号"
        />
      </search-item>
      <search-item>
        <PDInput
          v-model="queryParams.name"
          input-width="200px"
          description="姓名"
        />
      </search-item>
      <search-item>
        <PDInput
          v-model="queryParams.username"
          input-width="200px"
          description="账号"
        />
      </search-item>
      <search-item>
        <PDInput
          v-model="queryParams.phone"
          input-width="200px"
          description="手机号"
        />
      </search-item>
      <search-item
        label="单位名称"
      >
        <el-select
          v-model="queryParams.unitId"
          size="mini"
          clearable
          filterable
          @clear="queryParams.unitId = undefined"
        >
          <el-option
            v-for="item in unitList"
            :key="item.unitId"
            :label="item.name"
            :value="item.unitId"
          />
        </el-select>
      </search-item>
    </search-pane>

    <table-tool-bar
      v-model="showSearchPane"
      @handleCreate="handleCreateLabManager"
      @refresh="fetchLabManagerPage"
    />

    <table-card>
      <el-table
        ref="filterTable"
        :data="labManagerList"
        style="width: 100%"
      >
        <el-table-column
          prop="labManagerId"
          label="工号"
        />
        <el-table-column
          prop="name"
          label="名称"
        />

        <el-table-column
          prop="phone"
          label="手机号"
        />

        <el-table-column
          prop="unitName"
          label="所属单位"
        />

        <el-table-column
          prop="username"
          label="账号"
        />

        <el-table-column
          prop="state"
          label="状态"
        >
          <template slot-scope="scope">
            <el-tag
              :type="scope.row.status === 1 ? 'primary' : 'success'"
              disable-transitions
            >
              {{ scope.row.status === 1 ? "在职" : "出差" }}
            </el-tag>

          </template>
        </el-table-column>
        <el-table-column
          label="操作"
          fixed="right"
          width="200"
        >
          <template slot-scope="scope">
            <table-option-group
              type="button"
              @handleDelete="handleDeleteLabManager(scope.row.labManagerId)"
              @handleUpdate="handleUpdateLabManager(scope.row)"
            />
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="fetchLabManagerPage"
      />
    </table-card>

    <form-dialog
      v-model="dialogFormVisible"
      :type="formType"
      @create="createLabManager"
      @update="updateLabManager"
    >
      <el-form
        ref="labManagerForm"
        :rules="formRules"
        :model="tempLabManager"
      >
        <el-form-item prop="id" label="工号">
          <el-input
            v-model="tempLabManager.labManagerId"
            size="mini"
            style="width: 300px"
          />
        </el-form-item>

        <el-form-item prop="name" label="姓名">
          <el-input
            v-model="tempLabManager.name"
            size="mini"
            style="width: 300px"
          />
        </el-form-item>

        <el-form-item prop="username" label="账号">
          <el-input
            v-model="tempLabManager.username"
            size="mini"
            style="width: 300px"
          />
        </el-form-item>

        <el-form-item prop="password" label="密码">
          <el-input
            v-model="tempLabManager.password"
            size="mini"
            style="width: 300px"
          />
        </el-form-item>

        <el-form-item prop="phone" label="手机号">
          <el-input
            v-model="tempLabManager.phone"
            size="mini"
            style="width: 300px"
          />
        </el-form-item>

        <el-form-item prop="unitId" label="单位：">
          <el-select
            v-model="tempLabManager.unitId"
            size="mini"
            placeholder="单位名称"
          >
            <el-option
              v-for="item in unitList"
              :key="item.unitId"
              :label="item.name"
              :value="item.unitId"
            />
          </el-select>
        </el-form-item>

        <el-form-item prop="status" label="状态">
          <el-radio-group v-model="tempLabManager.status" size="medium">
            <el-radio border :label="1">在职</el-radio>
            <el-radio border :label="0">出差</el-radio>
          </el-radio-group>
        </el-form-item>

      </el-form>
    </form-dialog>
  </div>
</template>

<script>
import SearchPane from '@/components/SearchPane/SearchPane'
import SearchItem from '@/components/SearchPane/SearchItem'
import PDInput from '@/components/SearchPane/PDInput'
import TableCard from '@/components/table/TableCard'
import TableToolBar from '@/components/table/TableToolBar'
import { addLabManager, deleteLabManager, pageLabManagers, updateLabManager } from '@/api/lab-manager'
import FormDialog from '@/components/FormDialog/FormDialog'
import TableOptionGroup from '@/components/table/TableOptionGroup'
import { showSuccessTip } from '@/utils/simple-tips'
import Pagination from '@/components/Pagination'
import AttrSelect from '@/components/AttrSelect/AttrSelect'
import { listUnits } from '@/api/unit'

export default {
  name: 'LabManager',
  components: { Pagination, AttrSelect, TableOptionGroup, FormDialog, TableToolBar, TableCard, PDInput, SearchItem, SearchPane },
  data() {
    return {
      // search attr
      showSearchPane: true,
      queryParams: {
        pageSize: 5,
        pageNum: 1
      },
      total: 10,

      // form attr
      tempLabManager: {

      },
      dialogFormVisible: false,
      formType: '',
      textMap: {
        update: '修改',
        create: '添加'
      },
      formRules: {

      },

      // table attr
      labManagerList: [],

      // common attr
      unitList: []
    }
  },
  mounted() {
    this.fetchLabManagerPage()
    this.fetchUnitList()
  },
  methods: {

    fetchLabManagerPage() {
      pageLabManagers(this.queryParams).then(response => {
        this.labManagerList = response.data.list
        this.total = response.data.total
      })
    },
    fetchUnitList() {
      listUnits().then(response => {
        this.unitList = response.data
      })
    },

    // region search relative function
    handleSearchLabManager() {
      this.fetchLabManagerPage()
    },
    resetLabManagerQueryParams() {
      this.queryParams = {
        pageSize: 5,
        pageNum: 1
      }
    },
    // endregion

    // region form relative function
    resetTempLabManager() {
      this.tempLabManager = {}
    },
    handleCreateLabManager() {
      this.resetTempLabManager()
      this.formType = 'create'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['labManagerForm'].clearValidate()
      })
    },
    createLabManager() {
      this.$refs['labManagerForm'].validate((valid) => {
        if (valid) {
          addLabManager(this.tempLabManager).then(() => {
            this.fetchLabManagerPage()
            this.dialogFormVisible = false
            showSuccessTip()
          })
        }
      })
    },
    handleUpdateLabManager(row) {
      this.tempLabManager = Object.assign({}, row)
      this.formType = 'update'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['labManagerForm'].clearValidate()
      })
    },
    updateLabManager() {
      this.$refs['labManagerForm'].validate((valid) => {
        if (valid) {
          const tempLabManager = Object.assign({}, this.tempLabManager)
          updateLabManager(tempLabManager).then(() => {
            this.fetchLabManagerPage()
            this.dialogFormVisible = false
            showSuccessTip()
          })
        }
      })
    },
    handleDeleteLabManager(id) {
      this.$confirm('此操作将永久删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteLabManager(id).then(() => {
          this.dialogFormVisible = false
          this.fetchLabManagerPage()
          showSuccessTip()
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
    // endregion
  }
}

</script>

<style lang="scss" scoped>
</style>
